'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;
const {
	getCommonTime,
	getFullTime
} = require('api-time')
exports.main = async (event, context) => {
	let rechargeDatas = await db.collection('wmzmall-shopie').where({
		is_user_report: dbCmd.exists(false),
		share_user_id: dbCmd.exists(true)
	}).limit(100).orderBy('create_time', 'desc').get()
	rechargeDatas = rechargeDatas.data
	let app_id
	let shopieReportData
	let money = 0
	let refound_money = 0
	let refound_count = 0
	let pay_count = 0
	let wechat_count = 0
	let alipay_count = 0
	let wechat_money = 0
	let alipay_money = 0
	let sm_count = 0
	let sm_money = 0
	let lj_count = 0
	let lj_money = 0
	let dd_count = 0
	let dd_money = 0
	let cz_count = 0
	let cz_money = 0
	let hy_count = 0
	let hy_money = 0

	let type
	let date_time
	let appletInfo
	for (var i in rechargeDatas) {
		money = 0
		pay_count = 0
		refound_money = 0
		refound_count = 0
		wechat_count = 0
		alipay_count = 0
		wechat_money = 0
		alipay_money = 0
		sm_count = 0
		sm_money = 0
		lj_count = 0
		lj_money = 0
		dd_count = 0
		dd_money = 0
		cz_count = 0
		cz_money = 0
		hy_count = 0
		hy_money = 0
		app_id = rechargeDatas[i].app_id
		type = rechargeDatas[i].type
		if (type === 1) {
			money = 0
			refound_count = 1
			refound_money = rechargeDatas[i].money
		} else {
			refound_money = 0
			pay_count = 1
			money = rechargeDatas[i].money
			if (rechargeDatas[i].way === '微信支付') {
				wechat_count = 1
				wechat_money = money
			} else if (rechargeDatas[i].way === '支付宝支付') {
				alipay_count = 1
				alipay_money = money
			}
			if (rechargeDatas[i].cate == '收款码订单') {
				sm_count = 1
				sm_money = money
			} else if (rechargeDatas[i].cate == '远程收单订单') {
				lj_count = 1
				lj_money = money
			} else if (rechargeDatas[i].cate == '商城订单') {
				dd_count = 1
				dd_money = money
			} else if (rechargeDatas[i].cate == '充值') {
				cz_count = 1
				cz_money = money
			} else if (rechargeDatas[i].cate == '会员') {
				hy_count = 1
				hy_money = money
			}
		}
		let create_time = rechargeDatas[i].create_time
		let cDate = getCommonTime(new Date(create_time));
		let cYear = cDate.now.year;
		let cMonth = cDate.now.month;
		let cDay = cDate.now.day;
		let cHours = cDate.now.hour;
		appletInfo = await db.collection('wmz-applet-info').doc(rechargeDatas[i].app_id).get()
		appletInfo = appletInfo.data[0]
		if (appletInfo.report_end_time && appletInfo.report_end_time != '23:59:59') {
			let tHours = parseInt(appletInfo.report_end_time.split(":")[0])
			if (cHours <= tHours) {
				// console.log('进入')
				cDate=new Date(create_time)
				cDate.setDate(cDate.getDate() - 1)
				cDate = getCommonTime(cDate);
				cYear = cDate.now.year;
				cMonth = cDate.now.month;
				cDay = cDate.now.day;
				cHours = cDate.now.hour;
			} 
		}
		shopieReportData = await db.collection('wmzmall-shopie-user-report').where({
			app_id: app_id,
			shop_id: app_id,
			year: cYear,
			month: cMonth,
			day: cDay,
			share_user_id: rechargeDatas[i].share_user_id
		}).get()
		shopieReportData = shopieReportData.data
		if (shopieReportData.length === 0) {
			console.log('不存在')
			date_time = new Date(cYear + "-" + cMonth + "-" + cDay + ' 00:00:00');
			await db.collection('wmzmall-shopie-user-report').add({
				app_id: app_id,
				shop_id: app_id,
				year: cYear,
				month: cMonth,
				day: cDay,
				money: money,
				pay_count: pay_count,
				refound_money: refound_money,
				refound_count: refound_count,
				count: 1,
				create_time: new Date().getTime(),
				date_time: date_time.getTime(),
				data: [rechargeDatas[i]],
				ex_month_report: false,
				wechat_count: wechat_count,
				alipay_count: alipay_count,
				wechat_money: wechat_money,
				alipay_money: alipay_money,
				sm_count: sm_count,
				sm_money: sm_money,
				lj_count: lj_count,
				lj_money: lj_money,
				dd_count: dd_count,
				dd_money: dd_money,
				cz_count: cz_count,
				cz_money: cz_money,
				hy_count: hy_count,
				hy_money: hy_money,
				share_user_id: rechargeDatas[i].share_user_id
			})
		} else {
			console.log('已存在')
			shopieReportData = shopieReportData[0]
			let count = shopieReportData.count + 1
			let data = shopieReportData.data
			data.push(rechargeDatas[i])
			data.sort((a, b) => (a.create_time < b.create_time) ? 1 : -1);
			await db.collection('wmzmall-shopie-user-report').doc(shopieReportData._id).update({
				money: shopieReportData.money + money,
				count: count,
				data: data,
				refound_money: shopieReportData.refound_money + refound_money,
				pay_count: shopieReportData.pay_count + pay_count,
				refound_count: shopieReportData.refound_count + refound_count,
				ex_month_report: false,
				wechat_count: shopieReportData.wechat_count + wechat_count,
				alipay_count: shopieReportData.alipay_count + alipay_count,
				wechat_money: shopieReportData.wechat_money + wechat_money,
				alipay_money: shopieReportData.alipay_money + alipay_money,
				sm_count: shopieReportData.sm_count + sm_count,
				sm_money: shopieReportData.sm_money + sm_money,
				lj_count: shopieReportData.lj_count + lj_count,
				lj_money: shopieReportData.lj_money + lj_money,
				dd_count: shopieReportData.dd_count + dd_count,
				dd_money: shopieReportData.dd_money + dd_money,
				cz_count: shopieReportData.cz_count + cz_count,
				cz_money: shopieReportData.cz_money + cz_money,
				hy_count: shopieReportData.hy_count + hy_count,
				hy_money: shopieReportData.hy_money + hy_money
			})
		}
		await db.collection('wmzmall-shopie').doc(rechargeDatas[i]._id).update({
			is_user_report: true
		})
	}
	return event
};